package org.smile.auth.session;

import java.io.IOException;
import java.security.Principal;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public abstract class AbstractSession implements Session{
    /**
     * 最大非活动间隔 seconds
     */
    private  int maxInactiveInterval=60*60;
    /**
     * 创建时间
     */
    private Long creationTime;
    /**生成的token*/
    private String token;

    private  transient Manager manager;

    long lastAccessTime=System.currentTimeMillis();
    /**
     * 是否有效的
     */
    private boolean valid=true;

    public AbstractSession(Manager manager){
        this.manager=manager;
    }

    protected String id;

    @Override
    public void initToken(String token) {
        this.token=token;
    }

    public void setManager(Manager manager) {
        this.manager = manager;
    }

    @Override
    public String getAuthType() {
        Object obj= this.getAttribute(AUTH_TYPE);
        return obj==null?"":obj.toString();
    }

    @Override
    public void setAuthType(String authType) {
        this.setAttribute(AUTH_TYPE,authType);
    }

    @Override
    public long getCreationTime() {
        return this.creationTime;
    }

    @Override
    public long getCreationTimeInternal() {
        return 0;
    }

    @Override
    public void setCreationTime(long time) {
        this.creationTime=time;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public String getIdInternal() {
        return null;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public void setId(String id, boolean notify) {
        this.id=id;
    }

    @Override
    public long getThisAccessedTime() {
        return 0;
    }

    @Override
    public long getThisAccessedTimeInternal() {
        return 0;
    }

    @Override
    public long getLastAccessedTime() {
        return 0;
    }

    @Override
    public long getLastAccessedTimeInternal() {
        return 0;
    }

    @Override
    public long getIdleTime() {
        return 0;
    }

    @Override
    public long getIdleTimeInternal() {
        return 0;
    }

    @Override
    public Manager getManager() {
        return null;
    }

    @Override
    public int getMaxInactiveInterval() {
        return this.maxInactiveInterval;
    }

    @Override
    public void setMaxInactiveInterval(int interval) {
        this.maxInactiveInterval=interval;
    }

    @Override
    public void setNew(boolean isNew) {

    }

    @Override
    public Principal getPrincipal() {
        return null;
    }

    @Override
    public void setPrincipal(Principal principal) {

    }

    @Override
    public void setValid(boolean isValid) {
        this.valid=isValid;
        if(isValid){
            this.lastAccessTime=System.currentTimeMillis();
        }
    }

    @Override
    public boolean isValid() {
        if(this.valid){
            this.valid=System.currentTimeMillis()-this.lastAccessTime>maxInactiveInterval*1000;
        }
        return this.valid;
    }

    @Override
    public void access() {
        this.lastAccessTime=System.currentTimeMillis();
    }

    @Override
    public void addSessionListener(SessionListener listener) {

    }

    @Override
    public void endAccess() {

    }

    @Override
    public void expire() {
        this.valid=false;
    }

    @Override
    public Object getNote(String name) {
        return null;
    }

    @Override
    public Iterator<String> getNoteNames() {
        return null;
    }

    @Override
    public void recycle() {

    }

    @Override
    public void removeNote(String name) {

    }

    @Override
    public Object getValue(String name) {
        return this.getAttribute(name);
    }

    @Override
    public void setNote(String name, Object value) {

    }

    @Override
    public boolean isAttributeDistributable(String name, Object value) {
        return false;
    }

    /**
     * 持久化方法
     * @param out
     * @throws IOException
     */
    @Override
    public void writeObjectData(java.io.ObjectOutputStream out) throws IOException {

    }

    /**
     * 持久化读取方法
     * @param in
     * @throws IOException
     * @throws ClassNotFoundException
     */
    @Override
    public void readObjectData(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {

    }

    public void setCreationTime(Long creationTime) {
        this.creationTime = creationTime;
    }

    @Override
    public String getToken() {
        return token;
    }

    @Override
    public void setToken(String token) {
        this.token = token;
    }
}
